/**
 * Copyright (c) 2008, Keisuke Inoue, Syracuse Unviersity
 * All rights reserved.
 *
 */
package edu.syr.iis.web.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import edu.syr.iis.web.model.AdjacencyManager;
import edu.syr.iis.web.model.Assignment;
import edu.syr.iis.web.model.AssignmentManager;
import edu.syr.iis.web.model.Conversation;
import edu.syr.iis.web.model.ConversationLabel;
import edu.syr.iis.web.model.ConversationLabelManager;
import edu.syr.iis.web.model.ConversationManager;
import edu.syr.iis.web.model.Dataset;
import edu.syr.iis.web.model.DialogueActDimension;
import edu.syr.iis.web.model.DialogueActDimensionManager;
import edu.syr.iis.web.model.DialogueActLabelManager;
import edu.syr.iis.web.model.Message;
import edu.syr.iis.web.model.User;
import edu.syr.iis.web.model.UserManager;
import edu.syr.iis.web.util.Key;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.mvc.SimpleFormController;

/**
 * @author kinoue
 *
 */
public class ConversationListController  extends AbstractController 
{
    ConversationManager conversationManager;
    ConversationLabelManager conversationLabelManager;
    private String successView;
    
	@Override
	public ModelAndView handleRequestInternal(HttpServletRequest request,
		    HttpServletResponse response) throws Exception
	{
		HashMap<String, Object> model = new HashMap<String,Object>();
		
		// For the dropdown list
		List<ConversationLabel> conversationLabels = conversationLabelManager.getConversationLabels();
		// For displaying "users currently online"
		
		User user = (User) request.getSession().getAttribute(Key.USER);

		if (user.getRole() == User.ADMIN
				&& request.getParameter(Key.DATASET_ID) != null
				&& request.getParameter(Key.DATASET_ID).trim().length() > 0)
		{		
			int datasetId = Integer.parseInt(request.getParameter(Key.DATASET_ID));
			List <Conversation> conversations = null;
			
			if (request.getParameter(Key.CLABEL_ID) != null
					&& request.getParameter(Key.CLABEL_ID).trim().length() > 0)
			{
				int conversationLabelId = Integer.parseInt(request.getParameter(Key.CLABEL_ID));

				// -1 is for "none", which is also the default 
				//
				if (conversationLabelId != -1 ) {
					if (conversationLabelId == 0)
						conversations = conversationManager.getConversations(datasetId);
					else	
						conversations = conversationManager.getConversations(datasetId, conversationLabelId);
				}
			} 
			model.put(Key.CONVS, conversations);
		} 					
		model.put(Key.CLABELS, conversationLabels);
		
		return new ModelAndView(successView, model);
	}
	
	public void setConversationManager(ConversationManager conversationManager) {
		this.conversationManager = conversationManager;
	}

	public void setConversationLabelManager(ConversationLabelManager conversationLabelManager)
	{
		this.conversationLabelManager = conversationLabelManager;
	}

	public void setSuccessView(String successView) {
		this.successView = successView;
	}
	
	
}
